package com.mjk.common.web.getway.restful.prototal;

import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.DefaultInterceptorChain;
import com.mjk.common.tools.interceptor.Interceptor;
import com.mjk.common.tools.springextend.BeeApplicationListener;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.transfer.GetwayTransfer;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * 
 * @author mjk
 *
 */
public class BaseGetwayInterceptor implements BeeApplicationListener {
	/**
	 * 导入文件的目录
	 */
	@Value("${bee.upload.path:}")
	public String UPLOAD_PATH;	
	/**
	 * 默认下载的文件名称
	 */
	@Value("${bee.export.file:down.csv}")
	protected String DEFAULT_FILE_NAME ;
	
	/**
	 * 协议转化类
	 */
	@Autowired
	protected GetwayTransfer getWayProtocal;
	/**
	 * 拦截器类型 对应的拦截链
	 */
	@Getter
    protected Map<String, DefaultInterceptorChain<GetWayInterceptorReq, BeeResponse>> chains;
	
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		//初始化过滤链条
		Map<String, GetWayInterceptor> interceptors = applicationContext.getBeansOfType(GetWayInterceptor.class);
		chains = GetWayInterceptorType.getChains();
		for(Entry<String,DefaultInterceptorChain<GetWayInterceptorReq,BeeResponse>> entry:chains.entrySet()){
			chains.get(entry.getKey()).setInterceptors(getInterceptor(interceptors, entry.getKey()));
		}
	}

	public List<Interceptor<GetWayInterceptorReq,BeeResponse>> getInterceptor(Map<String,GetWayInterceptor> interceptors,
																			  String type){
		return interceptors.values().stream()
				.sorted((u1,u2)->{
					BeeGetWayInterceptor order1 =u1.getClass().getAnnotation(BeeGetWayInterceptor.class);
					BeeGetWayInterceptor order2 =u2.getClass().getAnnotation(BeeGetWayInterceptor.class);
					int value1 = order1==null?0:order1.value();
					int value2 = order2==null?0:order2.value();
					return value2-value1;
				})//过滤BeeGetWayInterceptor 中flag为false的
				.filter(interceptor->
					interceptor.getClass().getAnnotation(BeeGetWayInterceptor.class)!=null && 
					interceptor.getClass().getAnnotation(BeeGetWayInterceptor.class).flag()	
				)//过滤类型
				.filter(interceptor->{
						BeeGetWayInterceptor annotaion = interceptor.getClass().getAnnotation(BeeGetWayInterceptor.class);
						GetWayInterceptorType[] types = annotaion.interceptorType();
						boolean isfilter = false;
						for(GetWayInterceptorType tmp : types){						
							if(tmp.getProto().equals(type)||(tmp.compareTo(GetWayInterceptorType.ALL)==0)){
								isfilter = true;
								break;
							}							
						}			
						return isfilter;					
					}
				)
				.collect(Collectors.toList());
	}

    public void setChains(
			Map<String, DefaultInterceptorChain<GetWayInterceptorReq, BeeResponse>> chains) {
		this.chains = chains;
	}	
}
